<template>
	<view class="page tn-safe-area-inset-bottom" :style="{paddingTop: vuex_custom_bar_height + 'px'}">
		<view class="top-bg"></view>
		<dawnBar title="资讯详情"></dawnBar>

		<view class="page-container">
			<view class="tn-bg-white dawn-shadow dawn-radius" v-if="articleInfo.title">
				<view class='newsDetail'>
					<view class='title'>{{ articleInfo.title }}</view>
					<view class='item tn-flex tn-flex-row-left tn-flex-col-center'>
						<view v-if="articleInfo.author" class='tn-margin-right-xs'>{{ articleInfo.author }}</view>
						<view class='tn-margin-right-xs'><text
								class='tn-icon-time-fill tn-padding-right-xs'></text>{{ formatDate(articleInfo.createTime) }}
						</view>
						<view class='tn-margin-right-xs'><text
								class='tn-icon-footprint tn-padding-right-xs'></text>{{ articleInfo.views }}
						</view>
					</view>
					<view class='conters'>
						<jyf-parser :html="content" ref="article" :tag-style="tagStyle" />
					</view>
				</view>
				<shareInfo @setShareInfoStatus="setShareInfoStatus" :shareInfoStatus="shareInfoStatus" />
			</view>
		</view>
		
		<!-- loading -->
		<dawnLoading :show="loading"></dawnLoading>
		
		<view v-if="show">
			<dawnMsg :msg="msg"></dawnMsg>
		</view>
		
		<!-- 悬浮按钮-->
		<NavBtn></NavBtn>
	</view>
</template>

<script>
	import page_mixin from '@/mixins/page_mixin.js'
	import shareInfo from '@/hosp/components/shareInfo/index.vue';
	import parser from "@/hosp/components/jyf-parser/jyf-parser";
	import * as ArticleApi from '@/api/cms/article.js';
	import dayjs from "@/plugin/dayjs/dayjs.min.js";
	import * as Util from '@/utils/util.js';
	import {
		mapGetters
	} from "vuex";
	const app = getApp();

	export default {
		name: 'newsDetail',
		components: {
			shareInfo,
			"jyf-parser": parser
		},
		mixins: [page_mixin],
		computed: mapGetters(['uid', 'isLogin', 'userInfo']),
		data() {
			return {
				loading: false,
				show: false,
				msg: '暂无数据',
				id: 0,
				articleInfo: [],
				store_info: {},
				content: '',
				tagStyle: {
					img: 'width:100%;'
				},
				shareInfoStatus: false,
			};
		},
		/**
		 * 生命周期函数--监听页面加载
		 */
		onLoad: function(options) {
			if (options.hasOwnProperty('id')) {
				this.id = options.id;
			} else {
				// #ifndef H5
				uni.navigateBack({
					delta: 1
				});
				// #endif
				// #ifdef H5
				history.back();
				// #endif
			}
		},
		onShow: function() {
			this.getArticleOne();
		},
		/**
		 * 用户点击右上角分享
		 */
		// #ifdef MP
		onShareAppMessage: function() {
			return {
				title: this.articleInfo.title,
				imageUrl: this.articleInfo.picUrl ? this.articleInfo.picUrl : "",
				desc: this.articleInfo.introduction,
				path: '/pages/news/detail?id=' + this.id
			};
		},
		// #endif
		methods: {
			getArticleOne: function() {
				this.loading = true;
				this.show = false;
				ArticleApi.getArticle(this.id).then(res => {
					console.table(res)
					this.loading = false;
					if (res.code === 0) {
						this.$set(this, 'articleInfo', res.data);
						this.content = res.data.content;
						// #ifdef H5
						if (this.$wechat.isWeixin()) {
							this.setShareInfo();
						}
						// #endif
					} else {
						this.show = true;
						this.msg = '暂无数据';
					}
				});
				// 增加文章浏览量
				// ArticleApi.addArticleBrowseCount(this.id)
			},
			goodInfo(id) {
				ProductSpuApi.getSpuDetail(id).then(res => {
					this.$set(this, 'store_info', res.data);
				})
			},
			listenerActionSheet() {
				this.shareInfoStatus = true
			},
			setShareInfoStatus() {
				this.shareInfoStatus = false
			},
			setShareInfo: function() {
				let href = location.href;
				let configAppMessage = {
					title: this.articleInfo.title,
					desc: this.articleInfo.introduction,
					link: href,
					imgUrl: this.articleInfo.picUrl
				};
				this.$wechat.wechatEvevt([
					"updateAppMessageShareData",
					"updateTimelineShareData"
				], configAppMessage);
			},

			fen2yuan(price) {
				return Util.fen2yuan(price)
			},
			formatDate: function(date) {
				return dayjs(date).format("YYYY-MM-DD");
			}
		}
	}
</script>

<style lang="scss" scoped>
	.newsDetail {
		padding: 30rpx 0;
	}

	.newsDetail .title {
		padding: 0 30rpx;
		font-size: 34rpx;
		color: #282828;
		font-weight: bold;
		line-height: 1.5;
	}
	
	.newsDetail .item {
		padding: 20rpx 30rpx;
		// font-size: 34rpx;
		color: #8A8B8C;
		// font-weight: bold;
		line-height: 1.5;
	}

	.newsDetail .conters {
		padding: 30rpx;
		font-size: 30rpx;
		color: #282828;
		line-height: 1.7;
	}

	.newsDetail .bnt {
		color: #fff;
		font-size: 30rpx;
		width: 690rpx;
		height: 90rpx;
		border-radius: 45rpx;
		margin: 48rpx auto;
		text-align: center;
		line-height: 90rpx;
	}
</style>